//
//  CollectViewController.m
//  LOL_JewelBox
//
//  Created by dllo on 15/5/1.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CollectViewController.h"
#import "HMSegmentedControl.h"
#import "NewsView.h"
#import "CollectModel.h"
#import "DetailCollectViewController.h"

@interface CollectViewController ()
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *newsArr;
@property(nonatomic,retain)NSMutableArray *competitionArr;
@property(nonatomic,retain)NSMutableArray *LolpictureArr;
@property(nonatomic, retain)NSMutableArray *belleArr;
@property(nonatomic,retain)NSMutableArray *postArr;
@property(nonatomic, retain)UIImageView *pictureImage;
@property(nonatomic, copy)NSString *documentNewStr;
@property(nonatomic, copy)NSString *documentComStr;
@property(nonatomic, copy)NSString *documentLolpicStr;
@property(nonatomic, copy)NSString *documentBelleStr;
@property(nonatomic, copy)NSString *documentPostStr;
@property(nonatomic, retain)HMSegmentedControl *segmentedControl;

@end

@implementation CollectViewController

- (void)dealloc
{
    [_newsArr release];
    [_competitionArr release];
    [_LolpictureArr release];
    [_belleArr release];
    [_postArr release];
    [_tableView release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.newsArr = [NSMutableArray array];
        self.competitionArr = [NSMutableArray array];
        self.LolpictureArr = [NSMutableArray array];
        self.belleArr = [NSMutableArray array];
        self.postArr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NewsView *newsView = [[NewsView alloc]initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT)];
    [self.view addSubview:newsView];
    newsView.tag = 1000;
    [newsView release];
#pragma mark 背景图片
    self.pictureImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 30, VIEWWIDTH, VIEWHEIGHT - 94 - 49)];
    self.pictureImage.image = [UIImage imageNamed:@"天使"];
    [newsView addSubview:self.pictureImage];
    [_pictureImage release];

    self.segmentedControl = [[HMSegmentedControl alloc]initWithSectionTitles:@[@"新闻", @"赛事", @"囧图", @"美女", @"神贴"]];
    self.segmentedControl.frame = CGRectMake(0, 0, VIEWWIDTH, 30);
    self.segmentedControl.layer.borderWidth = 0;
    [self.segmentedControl addTarget:self action:@selector(segClick:) forControlEvents:UIControlEventValueChanged];
    [self.segmentedControl setTextColor:[UIColor blackColor]];
    //    [self.segmentedControl setSelectionIndicatorColor:[UIColor blueColor]];
    [self.segmentedControl setBackgroundColor:[UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:0.98]];
    [newsView addSubview:self.segmentedControl];
    [_segmentedControl release];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, VIEWWIDTH, VIEWHEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = NO;
    self.tableView.rowHeight = 70;
      self.tableView.backgroundView = self.pictureImage;
    [_tableView release];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(Click:)];
#pragma mark 新闻,赛事,囧图,美女,神贴反归档方法
    [self Detailnews];
    [self Detailcompetition];
    [self DetailLolPicture];
    [self DetailBelle];
    [self DetailPost];
}


#pragma mark 控制按钮
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
#pragma mark 添加删除置顶

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *addAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        if (self.segmentedControl.selectedIndex == 0) {
            //更新数据
            [self.newsArr exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
            [NSKeyedArchiver archiveRootObject:self.newsArr toFile:self.documentNewStr];
        }else if (self.segmentedControl.selectedIndex == 1){
            [self.competitionArr exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
            [NSKeyedArchiver archiveRootObject:self.competitionArr toFile:self.documentComStr];
          
        }else if (self.segmentedControl.selectedIndex == 2){
            [self.LolpictureArr exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
           [NSKeyedArchiver archiveRootObject:self.LolpictureArr toFile:self.documentLolpicStr];
           
        }else if (self.segmentedControl.selectedIndex == 3){
            [self.belleArr exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
            [NSKeyedArchiver archiveRootObject:self.belleArr toFile:self.documentBelleStr];
           
        }else if (self.segmentedControl.selectedIndex == 4){
            [self.postArr exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
            [NSKeyedArchiver archiveRootObject:self.postArr toFile:self.documentPostStr];
        }
        //更新UI
        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        [self.tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
    }];
    addAction.backgroundColor = [UIColor redColor];
    
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        if (self.segmentedControl.selectedIndex == 0) {
            //更新数据
            [self.newsArr removeObjectAtIndex:indexPath.row];
            [NSKeyedArchiver archiveRootObject:self.newsArr toFile:self.documentNewStr];
        }else if (self.segmentedControl.selectedIndex == 1){
            [self.competitionArr removeObjectAtIndex:indexPath.row];
            [NSKeyedArchiver archiveRootObject:self.competitionArr toFile:self.documentComStr];
            
        }else if (self.segmentedControl.selectedIndex == 2){
            [self.LolpictureArr removeObjectAtIndex:indexPath.row];
            [NSKeyedArchiver archiveRootObject:self.LolpictureArr toFile:self.documentLolpicStr];
           
        }else if (self.segmentedControl.selectedIndex == 3){
            [self.belleArr removeObjectAtIndex:indexPath.row];
            [NSKeyedArchiver archiveRootObject:self.belleArr toFile:self.documentBelleStr];
            
        }else if (self.segmentedControl.selectedIndex == 4){
            [self.postArr removeObjectAtIndex:indexPath.row];
            [NSKeyedArchiver archiveRootObject:self.postArr toFile:self.documentPostStr];
          
        }
        //更新数据
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
    }];
    deleteAction.backgroundColor = [UIColor orangeColor];
    return @[addAction,deleteAction];
}

#pragma mark 移动

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSInteger  index = self.segmentedControl.selectedIndex;
    if (index == 0) {
        CollectModel *collectMo = [self.newsArr[sourceIndexPath.row]retain];
        [self.newsArr removeObjectAtIndex:sourceIndexPath.row];
        [self.newsArr insertObject:collectMo atIndex:destinationIndexPath.row];
        [NSKeyedArchiver archiveRootObject:self.newsArr toFile:self.documentNewStr];
        [collectMo release];
    }else if (index == 1){
        CollectModel *collectMo = [self.competitionArr[sourceIndexPath.row]retain];
        [self.competitionArr removeObjectAtIndex:sourceIndexPath.row];
        [self.competitionArr insertObject:collectMo atIndex:destinationIndexPath.row];
        [NSKeyedArchiver archiveRootObject:self.competitionArr toFile:self.documentComStr];
        [collectMo release];
    }else if (index == 2){
        CollectModel *collectMo = [self.LolpictureArr[sourceIndexPath.row]retain];
        [self.LolpictureArr removeObjectAtIndex:sourceIndexPath.row];
        [self.LolpictureArr insertObject:collectMo atIndex:destinationIndexPath.row];
        [NSKeyedArchiver archiveRootObject:self.LolpictureArr toFile:self.documentLolpicStr];
        [collectMo release];
    }else if (index == 3){
        CollectModel *collectMo = [self.belleArr[sourceIndexPath.row]retain];
        [self.belleArr removeObjectAtIndex:sourceIndexPath.row];
        [self.belleArr insertObject:collectMo atIndex:destinationIndexPath.row];
        [NSKeyedArchiver archiveRootObject:self.belleArr toFile:self.documentBelleStr];
        [collectMo release];
    }else if (index == 4){
        CollectModel *collectMo = [self.postArr[sourceIndexPath.row]retain];
        [self.postArr removeObjectAtIndex:sourceIndexPath.row];
        [self.postArr insertObject:collectMo atIndex:destinationIndexPath.row];
        [NSKeyedArchiver archiveRootObject:self.postArr toFile:self.documentPostStr];
        [collectMo release];
    }
}
#pragma mark 新闻收藏犯归档
- (void)Detailnews
{
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = [sandBox lastObject];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"收藏"];
    self.documentNewStr = [documentPath stringByAppendingPathComponent:@"collectNew.plist"];
        self.newsArr = [NSKeyedUnarchiver unarchiveObjectWithFile:self.documentNewStr];
}
#pragma mark 赛事收藏反归档
-(void)Detailcompetition
{
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = [sandBox lastObject];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"收藏"];
    self.documentComStr = [documentPath stringByAppendingPathComponent:@"collectCompetition.plist"];
    self.competitionArr = [NSKeyedUnarchiver unarchiveObjectWithFile:self.documentComStr];
}


#pragma mark 囧图收藏反归档
-(void)DetailLolPicture
{
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = [sandBox lastObject];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"收藏"];
   self.documentLolpicStr = [documentPath stringByAppendingPathComponent:@"collectLolPicture.plist"];
    self.LolpictureArr = [NSKeyedUnarchiver unarchiveObjectWithFile:self.documentLolpicStr];
}

#pragma mark 美女收藏反归档
-(void)DetailBelle
{
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = [sandBox lastObject];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"收藏"];
    self.documentBelleStr = [documentPath stringByAppendingPathComponent:@"collectBelle.plist"];
    self.belleArr = [NSKeyedUnarchiver unarchiveObjectWithFile:self.documentBelleStr];
}

#pragma mark 神贴收藏反归档

-(void)DetailPost
{
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = [sandBox lastObject];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"收藏"];
    self.documentPostStr = [documentPath stringByAppendingPathComponent:@"collectPost.plist"];
    self.postArr = [NSKeyedUnarchiver unarchiveObjectWithFile:self.documentPostStr];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger index = self.segmentedControl.selectedIndex;
    if (index == 0) {
        return self.newsArr.count;
    }else if (index == 1){
        return self.competitionArr.count;
    }else if (index == 2){
        return self.LolpictureArr.count;
    }else if (index == 3){
        return self.belleArr.count;
    }else if (index == 4){
        return self.postArr.count;
    }
    else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.segmentedControl.selectedIndex == 0) {
        static NSString *reuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse]autorelease];
        }
        CollectModel *collectMo = self.newsArr[indexPath.row];
        cell.textLabel.text = collectMo.title;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = NO;
        return cell;
    }else if (self.segmentedControl.selectedIndex == 1){
        static NSString *lala = @"lala";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:lala];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:lala]autorelease];
        }
        CollectModel *collectMo = self.competitionArr[indexPath.row];
        cell.textLabel.text = collectMo.title;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = NO;
        return cell;
    }else if (self.segmentedControl.selectedIndex == 2){
        static NSString *OO = @"OO";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OO];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:OO]autorelease];
        }
        CollectModel *collectMo = self.LolpictureArr[indexPath.row];
        cell.textLabel.text = collectMo.title;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = NO;
        return cell;
    }else if (self.segmentedControl.selectedIndex == 3){
        static NSString *We = @"We";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:We];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:We]autorelease];
        }
        CollectModel *collectMo = self.belleArr[indexPath.row];
        cell.textLabel.text = collectMo.title;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = NO;
        return cell;
    }else if (self.segmentedControl.selectedIndex == 4){
        static NSString *cc = @"cc";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cc];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cc]autorelease];
        }
        CollectModel *collectMo = self.postArr[indexPath.row];
        cell.textLabel.text = collectMo.title;
        cell.backgroundColor = [UIColor clearColor];
#pragma mark 点击cell不变色
        cell.selectionStyle = NO;
        return cell;
    }
    else
    {
        return 0;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger index    = self.segmentedControl.selectedIndex;
    DetailCollectViewController *detailCollect= [[DetailCollectViewController alloc]init];
    [self.navigationController pushViewController:detailCollect animated:YES];
    if (index == 0) {
        CollectModel *collectMo = self.newsArr[indexPath.row];
        detailCollect.url = collectMo.url;
    }else if (index == 1){
        CollectModel *collectMo = self.competitionArr[indexPath.row];
        detailCollect.url = collectMo.url;
    }else if (index == 2){
        CollectModel *collectMo = self.LolpictureArr[indexPath.row];
        detailCollect.url = collectMo.url;
    }else if (index == 3){
        CollectModel *collectMo = self.belleArr[indexPath.row];
        detailCollect.url = collectMo.url;
    }else if (index == 4){
        CollectModel *collectMo = self.postArr[indexPath.row];
        detailCollect.url = collectMo.url;
    }
    [detailCollect release];
}

-(void)segClick:(HMSegmentedControl *)seg
{
    if (seg.selectedIndex == 0) {
        [self Detailnews];
        [self.tableView reloadData];
    }else if (seg.selectedIndex == 1){
        [self Detailcompetition];
        [self.tableView reloadData];
    }else if (seg.selectedIndex == 2){
        [self DetailLolPicture];
        [self.tableView reloadData];
    }else if (seg.selectedIndex == 3){
        [self DetailBelle];
        [self.tableView reloadData];
    }else if (seg.selectedIndex == 4){
        [self DetailPost];
        [self.tableView reloadData];
    }
}

- (void)Click:(id)sander
{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
